<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <i class="fa fa-tachometer" aria-hidden="true"></i> 订单管理
      </h1>
    </section>
    
    <section class="content">
        <div class="box">
                <div class="box-header">
                    <h3 class="box-title">用户列表</h3>
                    <div class="box-tools">
                        <a class="btn btn-primary"  href="/orders/createProduct">创建生产订单</a>
                        <a class="btn btn-primary"  href="/orders/createAmazon">创建亚马逊出库订单</a>
                    </div>
                </div><!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
         
        

            <table class="table table-striped table-hover table-bordered table-condensed">
                <thead>
                    <tr>
                        <th>订单ID</th>
                        <th>下单时间</th>
                        <th>下单人</th>
                        <th>订单类型</th>
                        <th>订单状态</th>
                        <th>-</th>
                    </tr>
                </thead>
                <tbody>
                   <?php
                        if(!empty($orders))
                        {
                            foreach($orders as $record)
                            {
                        ?>
                        <tr>
                            <td><?php echo $record->id ?></td>
                            <td><?php echo date("d-m-Y", strtotime($record->createdDtm))  ?></td>
                            <td><?php echo $record->name ?></td>
                            <td><?php echo $record->order_type_name ?></td>
                            <td>
                                <?php echo $record->order_state_name  ?>
                                    <?php
                                    if($record->order_type == 2) {
                                        ?>
                                        <a href="<?= base_url().'orders/codes/'.$record->id; ?>" class="btn btn-sm btn-info">开始贴码</a>
                                    <?php }
                                    ?>
                                </td>
                            <td class="text-center">
                                <a class="btn btn-sm btn-primary" href="<?= base_url().'orders/'.($record->order_type == 1 ? 'createProduct' : 'createAmazon').'/'.$record->id; ?>">
                                查看</a>

                                <a class="btn btn-sm btn-danger" data-action="delete" href="javascript:void(0)" data-id="<?=$record->id?>">
                                删除</a>
                            </td>
                        </tr>
                        <?php
                            }
                        }
                        ?>
                    
                </tbody>
            </table>

         </div><!-- /.box-body -->
        <div class="box-footer clearfix">
            <?php echo $this->pagination->create_links(); ?>
        </div>
      </div><!-- /.box -->

         <div class="modal modal-primary fade" id="uploadGoodsModal">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选择Excel</h4>
              </div>
              <div class="modal-body">
                <div class="form-group">
                  <label for="exampleInputFile">产品Excel</label>
                  <input type="file" id="goodsExcelInput">
                  <p>请选择格式正确的亚马逊订单文件.</p>
                </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="uploadGoodsAndCreate">上传并创建商品</button>
              </div>
            </div>
            <!-- /.modal-content -->
          </div>
          <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->

    </section>

    <script type="text/javascript">
        $(function(){

            $('[data-action=delete]').on('click', function(){
                let id = $(this).attr('data-id');
                $.post('/orders/delete', {id: id}).then(function(){
                    window.location.reload();
                }, function(err){
                    alert('删除失败');
                })
            })
            $('#uploadGoodsBtn').on('click', function(){
                $('#uploadGoodsModal').modal('show');
            })

            $('.order-delete').on('click', function(){
                if(confirm('您正在尝试删除该订单， 请注意删除后数据不可找回， 是否继续？')){
                    $.post('/Goods/deleteGoods', {id: $(this).attr('data-id')}).then(function(res){
                        window.location.reload();
                    }, function(e) {
                        console.log(e)
                    })
                }   
            })

            $('#uploadGoodsAndCreate').on('click', function(){
                if($("#goodsExcelInput")[0].files.length < 1) return;
                var formData = new FormData();
                formData.append("amazonOrderExcel", $("#goodsExcelInput")[0].files[0]);
                $.ajax({
                    url: '/goods/uploadGoods',
                    data: formData,
                    type: 'post',
                    dataType: 'json',
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(data) {
                        console.log(123, data);
                        $('#uploadGoodsModal').modal('hide');
                        window.location.reload();

                    },
                    error: function(){
                        console.log('获取失败')
                    }
                })
            })

        })
    </script>
 </div>